﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Shapes;

namespace FoodMatch
{   
    class RandomGenerator
    {
        private static string[] PICS = { "Assets\\foods\\01.jpg", "Assets\\foods\\01.jpg", "Assets\\foods\\01.jpg", "Assets\\foods\\01.jpg", "Assets\\foods\\01.jpg", "Assets\\foods\\01.jpg", "Assets\\foods\\01.jpg", "Assets\\foods\\01.jpg" };

        private bool[,] tiles = new bool[GameState.ROW_NUM, GameState.COLUMN_NUM];
        private List<string> pics = new List<string>();
        private int freespace = GameState.TILE_NUM;
        Random rand = new Random();
        public RandomGenerator()
        {
            for (int i = 0; i < PICS.Length; i++)
            {
                pics.Add(PICS[i]);
            }
        }

        public BitmapImage generateImg()
        {
            if (pics.Count == 0 || freespace == 0)
                return null;
            int picIndex = rand.Next(pics.Count - 1);
            String rootPath = Windows.ApplicationModel.Package.Current.InstalledLocation.Path.ToString();
            IAsyncOperation<StorageFile> op = StorageFile.GetFileFromPathAsync(rootPath + "\\" + pics[picIndex]);
            StorageFile file = op.GetResults();
            IAsyncOperation<IRandomAccessStreamWithContentType> op2 =(file.OpenReadAsync());
            IRandomAccessStreamWithContentType fileStream = op2.GetResults();
            BitmapImage img = new BitmapImage();
            img.SetSource(file.OpenAsync(FileAccessMode.Read).GetResults());
            return img;
        }

        public int generateNum()
        {
            if (pics.Count == 0)
                return freespace;
            int num = rand.Next(freespace / 2) * 2;
            freespace -= num;
            return num;
        }

        public Point generatePosition()
        {
            int x;
            int y;
            do
            {
                x = rand.Next(GameState.ROW_NUM - 1);
                y = rand.Next(GameState.COLUMN_NUM - 1);
            } while (tiles[x, y]);
            return new Point(x, y);
        }

    }
}
